package email.becode.simultaneous_interpretation_android

import android.app.Application
import android.util.Log
import timber.log.Timber

class SimultaneousInterpretationApp : Application() {
    override fun onCreate() {
        super.onCreate()
        
        // 初始化 Timber
        Timber.plant(object : Timber.DebugTree() {
            override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
                // 将日志同时写入文件
                val priorityStr = when (priority) {
                    Log.VERBOSE -> "V"
                    Log.DEBUG -> "D"
                    Log.INFO -> "I"
                    Log.WARN -> "W"
                    Log.ERROR -> "E"
                    else -> "?"
                }
                
                try {
                    val logFile = getExternalFilesDir("logs")?.let { dir ->
                        if (!dir.exists()) dir.mkdirs()
                        java.io.File(dir, "app.log")
                    }
                    
                    logFile?.appendText("$priorityStr/$tag: $message\n")
                    if (t != null) {
                        logFile?.appendText("${t.stackTraceToString()}\n")
                    }
                } catch (e: Exception) {
                    super.log(Log.ERROR, "Timber", "Failed to write log to file: ${e.message}", e)
                }
                
                // 调用原始的日志方法
                super.log(priority, tag, message, t)
            }
        })
        
        Timber.i("应用启动")
    }
} 